package com.example.demo.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.entity.SysDept;
import com.example.demo.mapper.DeptMapper;
import com.example.demo.mapper.PeopleMapper;
import com.example.demo.utils.JDBCUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/dept")
public class DeptController extends BaseController{
    @Resource
    DeptMapper deptMapper;

    @Resource
    PeopleMapper peopleMapper;

    @PostMapping("/add")
    public Result<?> save(@RequestBody SysDept sysDept) {
        sysDept.setIsActive(true);
        if(sysDept.getType().equals("department")){
            SysDept sysDept1 = deptMapper.selectByOutId(sysDept.getParentId());
            sysDept.setParent(sysDept1.getName());
        }
        deptMapper.insert(sysDept);
        return Result.success();
    }

    @PostMapping("/update")
    public Result<?> update(@RequestBody SysDept sysDept) {
//        People people = peopleMapper.selectById(dept.getLeaderId());
//        dept.setLeaderName(people.getName());
//        dept.setLeaderPhone(people.getPhone());
        if(sysDept.getType().equals("department")){
            SysDept sysDept1 = deptMapper.selectByOutId(sysDept.getParentId());
            sysDept.setParent(sysDept1.getName());
        }
        deptMapper.updateById(sysDept);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable Long id) {
        deptMapper.deleteById(id);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result<?> getById(@PathVariable Long id) {
        return Result.success(deptMapper.selectById(id));
    }

    @GetMapping("/{departmentId}")
    public Result<?> getByDepartmentId(@PathVariable String departmentId) {
        return Result.success(deptMapper.selectByOutId(departmentId));
    }

    @GetMapping("/company/{departmentId}")
    public Result<?> getCompany(@PathVariable String departmentId){
        LambdaQueryWrapper<SysDept> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(SysDept::getOutId, departmentId);
        return Result.success(deptMapper.selectOne(wrapper));
    }

    @GetMapping("/list")
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search,
                              @RequestParam String type,
                              @RequestParam String company) {
        LambdaQueryWrapper<SysDept> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(SysDept::getIsActive, true);
        if (StrUtil.isNotBlank(search)) {
            wrapper.like(SysDept::getName, search);
        }
        if (StrUtil.isNotBlank(type)) {
            wrapper.like(SysDept::getType, type);
        }
        if (StrUtil.isNotBlank(company)) {
            wrapper.eq(SysDept::getParentId, company);
        }
        Page<SysDept> deptPage = deptMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(deptPage);
    }

    @GetMapping("/all")
    public Result<?> all() {
        return Result.success(deptMapper.selectList(null));
    }

    @GetMapping("/gh")
    public Result<?> allGh() {
        LambdaQueryWrapper<SysDept> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(SysDept::getParentId, "4bbfcee379ec4334b594c768e1769d7b");
        wrapper.eq(SysDept::getIsActive, true);
        return Result.success(deptMapper.selectList(wrapper));
    }

//    @PostMapping("/synchronise")
//    public Result<?> synchronise(){
//        try{
//            JDBCUtil.getDepartment();
//        }catch (Exception e){
//            e.printStackTrace();
//            return Result.error("400", "同步失败");
//        }
//        return Result.success();
//    }
}
